﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.WebApp.KM;
using ZX.DAL.DBExtend;
using System.Data;
using System.Data.Linq;
using ZX.CRM.Bussiness;

namespace ZX.WebApp.DeskTop.Form
{
    public partial class DesktopMain : KmBasePage
    {
        private string _Action = string.Empty;

        public string getRight {
            get
            {
                BCrmValue bv = new BCrmValue();
                string hasRight = "0";
                DataTable dt = bv.ReturnDsSort(this.UserId.Value.ToString(), "sp_desktopsort");
                if (dt != null && dt.Rows.Count > 0) hasRight = "1";
                return hasRight;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _Action = QueryString("Action");
            switch (_Action)
            {
                case "LoadData":
                    LoadData();
                    break;
            }
        }

        protected void LoadData()
        {
            //获取模块具体栏位布局信息
            //获取模块具体栏位布局信息
            DataSet ds = new DataSet();
            string strsql = @"SELECT DISTINCT CASE WHEN sud1.AddUser IS NULL 
	                            THEN 1 
	                            ELSE (CASE WHEN sud.UserDesktopID IS NULL THEN 0 ELSE 1 END)
	                            END AS IsChecked
                                ,sdt.Region
                                ,sud.AddUser
                                        ,srd.[DeskTopTempID]
                                        ,sdt.DeskTopTempName AS [DeskTopName]
                                        ,sdt.[Height]
                                        ,MIN(ISNULL(sud.Sort,srd.[Sort])) OVER(PARTITION BY srd.[DeskTopTempID]) AS Sort
                                        ,sdt.[Src]
                                        ,sdt.PageType
                                        ,MIN(Coalesce(srd.PowerType,sdt.PowerType,0)) OVER (PARTITION  BY sdt.DeskTopTempID) AS PowerType
                                        ,sdt.[TabTools]
                                        ,sdt.More
                                FROM SysRoleDesktop srd
		                            INNER JOIN SysDesktopTemplate sdt ON srd.DeskTopTempID=sdt.DeskTopTempID
		                            INNER JOIN SysUserRole sur ON srd.RoleID=sur.RoleID
		                            LEFT JOIN SysUserDesktop sud ON srd.DeskTopTempID=sud.RoleDesktopID AND sur.UserID=sud.AddUser
		                            LEFT JOIN SysUserDesktop sud1 ON sud1.AddUser='@UserID'
                                WHERE sur.UserID='@UserID'
                                ORDER BY IsChecked desc,Sort asc";
            strsql = strsql.Replace("@UserID", this.UserId.Value.ToString());
            strsql = strsql.Replace("@UserID", this.UserId.Value.ToString());
            DbSql execsql = new DAL.DBExtend.DbSql();
            ds = execsql.Query(strsql);

            List<DeskTopModel> list = new List<DeskTopModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                var r = 0;
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                     var ck=row["IsChecked"].ToString();
                     if (ck == "1")
                     {
                         DeskTopModel m = new DeskTopModel
                         {
                             id = row["DeskTopTempID"].ToString(),
                             src = row["SRC"].ToString(),
                             pageType = row["PageType"].ToString(),
                             powerType = row["PowerType"].ToString(),
                             title = row["DeskTopName"].ToString(),
                             sort = row["Sort"].ToString(),
                             tools = row["TabTools"].ToString(),
                             rowNumber = r,
                             Region = row["Region"].ToString(),
                             more = row["More"].ToString()
                         };
                         r++;
                         list.Add(m);
                     }
                }
            }
            ResponseJson(list);
        }
    }
}